package cn.edu.xmu.oomall.order.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.order.model.vo.NewNormalOrderPostVo;
import cn.edu.xmu.oomall.order.model.vo.OrderVo;
import cn.edu.xmu.oomall.order.model.vo.SimpleOrderRetVo;
import cn.edu.xmu.oomall.order.service.OrderService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@Api(value = "订单", tags = "order")
@RestController /*Restful的Controller对象*/
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
@Component
public class OrderController {
    @Autowired
    private HttpServletResponse httpServletResponse;

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "获取订单的所有状态", tags = "order")
    @ApiResponses(value = {
        @ApiResponse(code = 0, message = "成功")})
    @GetMapping(value = "/orders/states")
    public Object getorderState() {
        ReturnObject<List> returnObject = orderService.getOrderStates();
        return Common.decorateReturnObject(returnObject);
    }

//    @ApiOperation(value = "买家申请建立订单", tags = "order")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
//            @ApiImplicitParam(paramType = "body", dataType = "NewNormalOrderPostVo", name = "orderInfo", value = "指定新订单的资料", required = false)
//    })
//    @ApiResponses(value = {
//            @ApiResponse(code = 0, message = "成功"),
//            @ApiResponse(code = 904, message = "库存不足")})
//    @Audit(departName = "orders")
//    @PostMapping(value = "/orders")
//    public Object customerPostNewNormalOrder(@Validated  @RequestBody NewNormalOrderPostVo body, BindingResult bindingResult,
//                                             @LoginUser Long loginUserId, @LoginName String loginUserName) {
//        var filedErrors = Common.processFieldErrors(bindingResult, httpServletResponse);
//        if (filedErrors != null)
//            return filedErrors;
//
//        ReturnObject ret;
//        ret = orderService.addOrder(body, loginUserId, loginUserName);
//
//        return Common.decorateReturnObject(ret);
//    }

    @ApiOperation(value = "买家修改本人名下订单", tags = "order")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "OrderVo", name = "body", value = "操作字段 (状态)", required = true)})
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功"),
            @ApiResponse(code = 507, message = "当前状态禁止此操作"),
            @ApiResponse(code = 801, message = "订单地址费用变化")})
    @Audit(departName = "orders")
    @PutMapping(value = "/orders/{id}")
    public Object modifyOrder(@Validated @RequestBody OrderVo orderVo, BindingResult bindingResult,
                              @PathVariable Long id, @LoginUser Long loginUser, @LoginName String loginUserName) {
        var filedErrors = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (filedErrors != null)
            return filedErrors;
        ReturnObject ret = orderService.updateOrder(id, orderVo, loginUser, loginUserName);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "买家取消本人名下订单", tags = "order")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "订单id", required = true)})
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功"),
            @ApiResponse(code = 507, message = "当前状态禁止此操作")})
    @Audit(departName = "orders")
    @PutMapping(value = "/orders/{id}/cancel")
    public Object cancelOrder(BindingResult bindingResult, @PathVariable Long id,
                              @LoginUser Long loginUser, @LoginName String loginUserName) {
        var filedErrors = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (filedErrors != null)
            return filedErrors;

        ReturnObject ret = orderService.cancelOrder(id, loginUser, loginUserName);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "店家对订单标记发货", tags = "order")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value = "商店id", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "OrderVo", name = "body", value = "指定发货资讯", required = true)})
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功"),
            @ApiResponse(code = 507, message = "当前状态禁止此操作")})
    @PutMapping(value = "/shops/{shopId}/orders/{id}/deliver")
    public Object postfreights(@Validated @RequestBody OrderVo orderVo, BindingResult bindingResult, @PathVariable Long id,
                               @PathVariable Long shopId, @LoginUser Long loginUser, @LoginName String loginUserName) {
        var filedErrors = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (filedErrors != null)
            return filedErrors;

        ReturnObject ret = orderService.postfreights(id, shopId, orderVo, loginUser, loginUserName);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "确认团购订单", tags = "order")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value = "商店id", required = true)})
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功"),
            @ApiResponse(code = 507, message = "当前状态禁止此操作")})
    @PutMapping(value = "/internal/shops/{shopId}/grouponorders/{id}/confirm")
    public Object confirmGroupOrders(BindingResult bindingResult, @PathVariable Long id, @PathVariable Long shopId,
                                     @LoginUser Long loginUser, @LoginName String loginUserName) {
        var filedErrors = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (filedErrors != null)
            return filedErrors;

        ReturnObject ret = orderService.confirmGroupOrders(id, shopId, loginUser, loginUserName);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "店家查询商户所有订单(概要)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value = "商户id(店员只能查询本店铺)", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "customerId", value = "查询的购买者用户id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orderSn", value = "按订单Sn来查询", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value = "开始时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value = "结束时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "页码", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页数目", required = false)})
    @ApiResponse(code = 0, message = "成功")
    @GetMapping(value = "/shops/{shopId}/orders")
    public Object shopGetAllSimpleOrders(@LoginUser Long loginUser, @PathVariable Long shopId, @RequestParam(required = false) Long customerId,
                                         @RequestParam(required = false) String orderSn, @RequestParam(required = false) String beginTime,
                                         @RequestParam(required = false) String endTime, @RequestParam(required = false, defaultValue = "1") Integer page,
                                         @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
       ReturnObject<PageInfo<Object>> ret = orderService.shopGetAllSimpleOrders(loginUser, page, pageSize);
       return Common.getPageRetVo(ret, SimpleOrderRetVo.class);
    }
}













